package com.ctg.payroll2.action.sysadmin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.ctg.tax.bean.po.TaxTable;
import com.ctg.payroll2.bean.po.PbCity;
import com.ctg.payroll2.bean.po.PbProvince;
import com.ctg.payroll2.service.*;
import com.ctg.payroll2.util.Constants;
import com.ctg.tax.service.TaxTableService;
import com.opensymphony.xwork2.ActionSupport;

public class ProvinceAction extends ActionSupport {
	private Page result; 
	private List<PbProvince> resultL;
	private int hasSubCity=0;
	public int getHasSubCity() {
		return hasSubCity;
	}

	public void setHasSubCity(int hasSubCity) {
		this.hasSubCity = hasSubCity;
	}

	public String queryProvince() {
		String PROVINCE_CODE = null;
		String PROVINCE_NAME = null;
		String PROVINCE_ID = null;
		Integer start = null;
		Integer limit = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		PROVINCE_CODE =  request.getParameter("PROVINCE_CODE"); 
		PROVINCE_NAME = request.getParameter("PROVINCE_NAME");
		PROVINCE_ID = request.getParameter("province_id");
		start = Parse.toInteger(request.getParameter("start") , 0);
		limit = Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE);
		//System.out.println(code  + "\t" + queryKey);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		try{
			ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbProvinceService");
			HashMap<String, String> hm=new HashMap<String, String>();
			hm.put("province_code", PROVINCE_CODE);
			hm.put("province_name", PROVINCE_NAME);
			hm.put("province_id", PROVINCE_ID);
			result = provinceCityService.getProvinceCity(hm , start, limit);
		}catch(Exception e){
			e.printStackTrace();
			//System.out.println();
		
		}	

		return SUCCESS;
	}
	
	public String queryProvinceList() {
		Integer city_id = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		city_id = Parse.toInteger(request.getParameter("city_id"));
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService =(ProvinceCityService) wac.getBean("pbProvinceService");
		resultL = provinceCityService.queryProvince();  
		return SUCCESS;		 
	}
	
	
	public String queryProvinceDetails() {
		Integer province_id = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		province_id = Parse.toInteger(request.getParameter("province_id"));
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbProvinceService");
		resultL = provinceCityService.query(province_id, null); 
		return SUCCESS;		 
	}
	
	public String addProvince() {
		String province = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		province = request.getParameter("province");
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbProvinceService");
		provinceCityService.insertByJson(province);
		return SUCCESS;
	}
	
	public String delProvince(){
		 String provinceId = null;
			HttpServletRequest request = ServletActionContext.getRequest();
			provinceId = request.getParameter("province_id");
			String[] array = splitStringToStringArray(provinceId);
			WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
			ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbProvinceService");
			List<PbProvince> list = new ArrayList();
			
			PbProvince o=null;
			try {
				for(int i=0;i<array.length;i++){
					 o=new PbProvince();
					 o.setProvince_id(Integer.parseInt(array[i]));
					 list.add(o);
				}
			  
			 if(provinceCityService.deleteProvince(list)){

			 }else{
				 hasSubCity=1;
			 }
			 
				
			} catch (SQLException e) {
			 
				e.printStackTrace();
			}
		return SUCCESS;
	}
	public static String[] splitStringToStringArray(String args) {
		JSONArray json = JSONArray.fromObject(args);
		Object[] ss = json.toArray();
		String[] array = new String[ss.length];
		for (int i = 0; i < ss.length; i++) {
			//System.out.println("====="+ss[i]);
			String s =  ss[i]+"";
			array[i] = s;
		}
		return array;
	}
	public String updateProvince() {
		String province = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		province = request.getParameter("province");
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbProvinceService");
		provinceCityService.updateByJson(province);
		return SUCCESS;
	}
	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}

	public List<PbProvince> getResultL() {
		return resultL; 
	}

	public void setResultL(List<PbProvince> resultL) {
		this.resultL = resultL;
	}
}
